<?php

class RegistrationController extends Controller
{
	public function actions()
	{
		return array(
			'captcha'=>array(
				'class'     => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
		);
	}
	
	public function actionIndex() {
		
		$model = new FormUserRegistration;
		
		$this->performAjaxValidation($model);
	 	
	    if (Yii::app()->user->id)
		{
	    	$this->redirect($this->module->urls['profile']);
	    }
		else
		{
			$this->pageTitle = UserModule::t('Registration');

			Yii::app()->menu->set('breadcrumbs', array(
				UserModule::t('Users') => array('/user'),
				$this->pageTitle
			));
			
	    	if(isset($_POST['FormUserRegistration']))
			{
				$model->attributes = $_POST['FormUserRegistration'];
				
				if($model->validate())
				{
					$user = new User();
					
					if($user)
					{
						$user->setScenario('registration');
						
						$user->attributes = $model->attributes;
						
						if($user->register())
						{
							if($user->status === User::STATUS_INACTIVE)
							{
								$activation_url = $this->createAbsoluteUrl(implode($this->module->urls['activation']), array('email' => $user->email, 'secret' => $user->secret));

								UserModule::sendMail(
									$user->email,
									UserModule::t('{sitename} - Registration', array('{sitename}' => Yii::app()->name)),
									UserModule::t('To activate your account, please visit {activationurl}.', array('{activationurl}' => $activation_url))
								);
							}

							if($this->module->registrationValidationType === UserModule::REGISTRATION_VALIDATION_TYPE_DIRECT)
							{
								Yii::app()->user->setFlash(
									'registration',
									UserModule::t(
										'Thank you for your registration. You may {login} now.',
										array('{login}' => CHtml::link(UserModule::t('login'), $this->module->urls['login']))
									)
								);
							}

							if($this->module->registrationValidationType === UserModule::REGISTRATION_VALIDATION_TYPE_EMAIL)
							{
								Yii::app()->user->setFlash(
									'registration',
									UserModule::t('Thank you for your registration. Please check your email for further instructions.')
								);
							}

							if($this->module->registrationValidationType === UserModule::REGISTRATION_VALIDATION_TYPE_ADMIN)
							{
								Yii::app()->user->setFlash(
									'registration',
									UserModule::t('Thank you for your registration. We will inform you once your account has been activated.')
								);
							}

							if($this->module->registrationValidationType === UserModule::REGISTRATION_VALIDATION_TYPE_EMAIL_ADMIN)
							{
								Yii::app()->user->setFlash(
									'registration',
									UserModule::t('Thank you for your registration. Please check your email for further instructions.')
								);
							}
						}
					}
					$this->refresh();
				}
			}
			$this->render('index', array('model' => $model));
		}
	}
	
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax'] === 'registration-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
}
